/**
 * Created with JetBrains WebStorm.
 * User: thaidoanhong
 * Date: 5/8/13
 * Time: 1:21 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 4/22/13
 * Time: 2:11 PM
 * To change this template use File | Settings | File Templates.
 */
var MyAudio = Class.create(MyElement, {
    initialize: function($super, o){
        $super(o);
        this.className = "MyAudio";
    },
    initAttributeList: function($super, o){
        $super(o);

        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "audio" && this.key == "src"){
                _this.attributeList.add(new MyAttribute(this));
                if(_this.audioTag == undefined){
                    _this.audioTag = new Element("audio");
                    _this.audioTag.setStyle({position: "absolute", left: "0", bottom: "-32px", width: "43px"});
                    _this.audioTag.controls = "controls";
                    //_this.audioTag.setStyle({width: "100%", height: "100%"});
                    $(_this.divTag).insert(_this.audioTag);
                }


                var sourceAudio = $(_this.audioTag).firstDescendant();
                if(sourceAudio == null){
                    sourceAudio = new Element("source");
                    _this.audioTag.insert(sourceAudio);
                }
                sourceAudio.setAttribute(this.key, this.value);

            }
            else if(this.type == "audio" && this.key == "icon"){
                _this.attributeList.add(new MyAttribute(this));

                var imgTag = new Element("img", {src: this.value});
                imgTag.setStyle({width: "100%", height: "100%", position: "absolute", top: "0", left: "0"})

                $j(_this.divTag).append(imgTag);
            }
        });
    },
    reloadAttributeList: function($super, o){
        $super(o);

        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "audio" && this.key == "src"){
                _this.attributeList.add(new MyAttribute(this));
                var sourceAudio = $(_this.audioTag).firstDescendant();
                sourceAudio.setAttribute(this.key, this.value);
            }
            else if(this.type == "audio" && this.key == "icon"){
                _this.attributeList.add(new MyAttribute(this));
                $j(_this.divTag).find("img").attr("src", this.value);
            }
        });
    },
    setValueByKey: function($super, key, value){
        if($super(key, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromKey(key);
            if(att !== null){
                var matched = false;
                if(att.type == "audio" && att.key == "src"){

                    var sourceAudio = $(this.audioTag).firstDescendant();
                    if(sourceAudio !== null){
                        $(sourceAudio).setAttribute(att.key, value);
                    }
                    // load new source
                    this.audioTag.load();
                    matched = true;
                }
                else if(att.type == "audio" && att.key == "icon"){
                    $j(this.divTag).find("img").attr("src", value);
                    matched = true;
                }
                if(matched){
                    this.attributeList.setValueByKey(key, value);
                    return true;
                }
            }
        }
        return false;
    },
    setValueByName: function($super, name, value){
        if($super(name, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromName(name);
            if(att !== null){
                var matched = false;
                if(att.type == "audio" && att.key == "src"){

                    var sourceAudio = $(this.audioTag).firstDescendant();
                    if(sourceAudio !== null){
                        $(sourceAudio).setAttribute(att.key, value);
                    }
                    // load new source
                    this.audioTag.load();
                    matched = true;
                }
                else if(att.type == "audio" && att.key == "icon"){
                    $j(this.divTag).find("img").attr("src", value);
                    matched = true;
                }
                if(matched){
                    this.attributeList.setValueByName(name, value);
                    return true;
                }
            }
        }
        return false;
    }
});